

import javax.microedition.lcdui.*;
import javax.microedition.io.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;

import java.io.*;
import java.util.Random;

class Play
    extends Canvas
    implements CommandListener, Runnable, PlayerListener {

    private Mobil midlet;
    private Display display;
    private Command cmExit;
    private int y = 0;
    private int px = 76;
    private int py = 180;
    private boolean yes = true;
    private String bgImgN = "/bg.gif";
    private Image bgImg = null;
    private int randRange = 20;
    private int[] bombX = new int[randRange];
    private int[] bombY = new int[randRange];
    private int xScreen = 176;
    private int yScreen = 208;
    private int ySkyEdge = 500;
    private int hplane = 23;
    private int wplane = 21;
    private int bombAmount = 0;
    private int whbomb = 15;
    private boolean first = true;
    private int minIndex = 0;
    private Random random;
    private boolean gameover = false;
    private Player player;
    private VolumeControl vc;
    private int gauge = 1000;


   
    public Play(Mobil midlet, Display display, Player player) {           
        random = new Random();
        this.midlet = midlet;
        this.display = display;
        this.player = player;
        cmExit = new Command("Keluar", Command.OK, 1);

        addCommand(cmExit);
        setCommandListener(this);

        try {
            bgImg = Image.createImage(bgImgN);
        } catch (java.io.IOException e) {
        }
    }
   
    protected void paint(Graphics g) {
        g.setClip(0,0,xScreen,yScreen);
        g.drawImage(bgImg,0,-y,Graphics.TOP|Graphics.LEFT);           
               
        g.setColor(255, 255, 255);
        g.fillRect(5, 5, 105, 10);

        int tmp = gauge;
        if(gauge > 1000){
            tmp = 100;
        }else{
            tmp = (int)((gauge * 100) / 1000);
        }

        g.setColor(245, 146, 103);
        g.fillRect(5, 5, (tmp + 5), 10);

        if(gameover == true){
            g.setColor(254, 22, 57);                       
            g.setFont(Font.getFont(Font.FACE_PROPORTIONAL,Font.STYLE_BOLD,Font.SIZE_LARGE));
            g.drawString("Created by LIGAR",40,((yScreen / 2) - 10), Graphics.TOP | Graphics.LEFT);
        }
        int xpic;
        int ypic;
        if((shooted() == true)||(gauge <= 0)){
            gameover = true;
            g.setClip((px - 2),(py - 2),35,35);       
            xpic = 119;
            ypic = 500;
            g.drawImage(bgImg,(px - 2) - xpic,-(ypic - (py - 2)),Graphics.TOP|Graphics.LEFT);           
        }else{               
            if((bombY[minIndex] >= (yScreen - 15))||(first == true)){
                generateBomb();
                if(first == true){
                    first = false;
                }
            }else{
                for(int i=0;i<bombAmount;i++) {
                    bombY[i]++;
                    if(bombY[i] > yScreen){
                        gauge = gauge + 3;
                    }
                }
            }
           
            xpic = 77;
            ypic = 509;
            for(int i=0;i<bombAmount;i++) {
                if(bombY[i] <= (yScreen - 15)){               
                    g.setClip(bombX[i],bombY[i],whbomb,whbomb);
                    g.drawImage(bgImg,bombX[i] - xpic,-(ypic - bombY[i]),Graphics.TOP|Graphics.LEFT);
                }
            }           
           
            g.setClip(px,py,hplane,wplane);       
            xpic = 15;
            ypic = 500;
            g.drawImage(bgImg,px - xpic,-(ypic - py),Graphics.TOP|Graphics.LEFT);   
        }
    }
    //created by  Ligar Sekar Wangi
    protected void keyPressed(int keyCode) {
            switch (keyCode) {
                /*case -1: // UP
                    py = py - 10;
                    if(py < 0){
                        py = py + 10;
                    }
                    break;
                case -2: // DOWN//
                    py = py + 10;
                    if(py > (yScreen - hplane)){
                        py = py - 10;
                    }
                    break;*/
                case -3: // LEFT//
                    px = px - 10;
                    if(px < 0){
                        px = px + 10;
                    }
                    break;
                case -4: // RIGHT
                    px = px + 10;
                    if(px > (xScreen - wplane)){
                        px = px - 10;
                    }
                    break;
            }
    }

    public  void commandAction(Command c, Displayable s) {
        if (c == cmExit) {
            yes = false;
            midlet.exitMIDlet();
        }
    }

    public void run() {
        while (gameover == false) {
            try {
                synchronized (this) {
                        if(y <= 0){
                            y = ySkyEdge - yScreen;
                        }else{                           
                            y--;
                        }
                        Thread.sleep(10);
                        gauge = gauge - 3;
                        repaint();
                }
            } catch (Exception ie) {
            }
        }
        if(gameover == true){
            bombAmount = 0;           
            try {
                Thread.sleep(1500);
                player.stop();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void generateBomb(){   
        bombAmount = random.nextInt() % randRange;
        if(bombAmount < 1){
            bombAmount = randRange;
        }
        random.setSeed(xScreen);
        for(int i=0;i<bombAmount;i++){
            int xBomb = random.nextInt() % (xScreen - 15);
            int yBomb = random.nextInt() % (yScreen - 40);
            if(xBomb < 0){
                xBomb = xBomb + (xScreen - 15);
            }
            bombX[i] = xBomb;
            bombY[i] = yBomb - (yScreen - 40);
        }
        minIndex = 0;
        int min = bombY[minIndex];
        for(int i=1;i<bombAmount;i++) {
            if(min > bombY[i]){
                min = bombY[i];
                minIndex = i;
            }
        }
    }

    boolean shooted(){
        boolean res = false;
        int i=0;
        while((res == false)&&(i < bombAmount)) {
            if(inPlaneRange(bombX[i], bombY[i]) == true){
                res = true;                   
                try {
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            i++;
        }
        return res;
    }

    boolean inPlaneRange(int x, int y){
        boolean res = false;
        if((x >= px)&&(x <= (px + wplane))&&(y >= py)&&(y <= (py + hplane))){
            res = true;
        }
        return res;
    }

    private void playMedia(String file)
        throws Exception {
        player = Manager.createPlayer(getClass().getResourceAsStream(file), "audio/x-wav");
   
        player.addPlayerListener(this);
   
        player.setLoopCount(-1);
        player.prefetch();
        player.realize();
       
        vc = (VolumeControl)player.getControl("VolumeControl");
        if (vc != null)
            vc.setLevel(30);

   
        player.start();
    }

    public void playerUpdate(Player player, String event, Object eventData) {
        if(event.equals(PlayerListener.STARTED) &&
          new Long(0L).equals((Long)eventData)) {
        } else if(event.equals(PlayerListener.CLOSED)) {
        }
    }
}
